// pages/adress/addadress/addadress.js
const app = getApp()
Page({
  data: {
    adressid:'',
    type:'',
    addressinfoi:'',
    tel:'',
    sex:'',
    userName:'',
    region: ['请选择收货地址'],
    customItem: '',
    key:3,
    status:0,
    regionval:'',
    inputShowed: false,
    url:'',
    items: [
      { value: '1', name: '男士', checked: '' },
      { value: '2', name: '女士', checked:''},
    ],
    itemstap: [
      { value: '0', name: '家庭' ,checked: ''},
      { value: '1', name: '学校' ,checked: ''},
      { value: '2', name: '公司', checked: ''},    
      { value: '3', name: '其他', checked: '' },
    ]
  },

  onLoad: function (options) {
   if(options.type=='edit'){
       wx.setNavigationBarTitle({
         title: '编辑地址'
       })
      var json = JSON.parse(options.json)
     this.data.items[Number(json.sex)-1].checked = "true";
     this.data.itemstap[Number(json.lable)].checked = "true";
      console.log(json)
      this.setData({
        adressid:json.id,
        type:'edit',
        url: app.globalData.url + '/jxb/oneself/updateAddress',
        sex: json.sex ,
        items: this.data.items,
        itemstap: this.data.itemstap,
        userName: json.contact,
        tel: json.tel,
        addressinfoi: json.addressinfo,
        status: json.status,
        lable: json.lable,
        region: [json.address]
          
      })
   }else{
     this.setData({
       url: app.globalData.url + '/jxb/oneself/addAddress',
     })
   }
   
  },
  radioChange(e){  //选择性别
    var that = this;
    var radioValue = e.detail.value;
    that.setData({
      sex: radioValue
    })
  },
  bindRegionChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let region = e.detail.value;
    if (e.detail.value){
      this.setData({
        region: region
      })
      console.log(this.data.region)   
    }
  },
  choosetap(e){   //选择标签
    console.log(e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id  
    for (var i = 0; i < this.data.itemstap.length;i++){
      this.data.itemstap[i].checked = "";    
    }
    this.data.itemstap[Number(id)].checked = "true";
    this.setData({
      key:id,
      itemstap: this.data.itemstap
    })
  },
  setadress(e){  //设置默认地址
    this.setData({
      status: this.data.status == 0 ? this.data.status = 1 : this.data.status = 0
    })
  },
  adInputChange(e){  //用户名
    this.data.userName = e.detail.value;
  },
  telChange(e) {  //手机号码
    this.data.tel = e.detail.value;
  },
  addressinfoi(e){
    this.data.addressinfoi = e.detail.value;
  },
  saveadrees:function(){ //添加地址
  if(this.check()){
    console.log(this.data.region.join(','))
    let data={};
    if(this.data.type=='edit'){
      data = {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        contact: this.data.userName,
        sex: this.data.sex,
        tel: this.data.tel,
        address: this.data.region.join(' '),
        addressinfo: this.data.addressinfoi,
        lable: this.data.key,
        status: this.data.status,
        remark: '',
        id: this.data.adressid
      };
    }else{
       data = {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        contact: this.data.userName,
        sex: this.data.sex,
        tel: this.data.tel,
        address: this.data.region.join(' '),
        addressinfo: this.data.addressinfoi,
        lable: this.data.key,
        status: this.data.status,
        remark: '',
      };
    }
    let _that = this;
    wx.request({   //传后台
      url: this.data.url,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: data,
      success: function (res) {
        if(res.data.code==0){
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          });
          _that.changeParentData();
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        }else{
          wx.showToast({
            title: '网络错误',
            icon: 'none',
            duration: 2000
          });
        }     
      },
      fail:function(){
        wx.showToast({
          title: '网络错误',
          icon: 'none',
          duration: 2000
        });
      }

    })
  }
  },
  changeParentData: function () {
    var pages = getCurrentPages();//当前页面栈
    if (pages.length > 1) {
      var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
      beforePage.getadress();//触发父页面中的方法
    }
  },
  check(){
    if (this.data.userName == '' || this.data.userName.match(/^\s+$/)){
      wx.showToast({
        title: '请填写联系人',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.sex == '') {
      wx.showToast({
        title: '请选择性别',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.tel == '') {
      wx.showToast({
        title: '请填写您的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      if (!(/^1[345789]\d{9}$/.test(this.data.tel))) {
        wx.showToast({
          title: '手机格式有误',
          icon: 'none',
          duration: 2000
        });
        return false;
      } else {
      }
    }
    if (this.data.region[0] == '请选择收货地址') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.addressinfoi == '' || this.data.addressinfoi.match(/^\s+$/)) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
   return true;

  }
})